<?php echo $this->Html->css('venta') ?>
<?php echo $this->Html->css('msg') ?>
<?php echo $this->Html->script('venta') ?>
<?php echo $this->Html->script('msg') ?>
<?php echo $this->Html->script('popup') ?>
<?php echo $this->Html->script('jquery.PrintArea') ?>

<div id="example" class="ventas">

    <fieldset>

        <section id="currentRenglon" class="currentRenglon">

            <?php echo $this->Form->input('currentPromo', array('label' => 'Promocion: ', 'id' => 'currentPromo', 'class' => 'name', 'readonly', 'div' => false)) ?>
            <?php echo $this->Form->input('quantity', array('label' => 'Cantidad: ', 'id' => 'quantity', 'div' => false)) ?>
            <?php echo $this->Form->input('currentPrice', array('label' => 'Precio: ', 'id' => 'currentPrice', 'readonly', 'div' => false)) ?>
            <?php echo $this->Form->input('currentTotal', array('label' => 'Total: ', 'id' => 'currentTotal', 'readonly', 'div' => false)) ?>

        </section>
        <p id="añadir">  Añadir</p>
        <p id="limpiar"> Limpiar</p>

    </fieldset>
    <hr>

    <div class="centerTables">

        <label>Seleccione el Promocion</label>
        <hr>

        <table cellpadding="0" cellspacing="0" id="tabla" class="tablaPromos">
            <tr class="titleTable" >
                <th><?php echo 'Promocion' ?></th>
                <th><?php echo 'Precio Unitario' ?></th>
                <th><?php echo 'Opciones' ?></th>
            </tr>
            <?php $i = 1; ?>
            <?php foreach ($promotions as $promotion): ?>
                <tr class="rowPromo" id="<?php echo $i ?>">
                    <td style="display: none"> <?php echo $promotion['Promotion']['id'] ?> </td>
                    <td> <?php echo $promotion['Promotion']['name'] ?> </td>
                    <td> <?php echo $promotion['Promotion']['sale_price'] ?> </td>
                    <td class="agregar">  </td>
                </tr>
                <?php $i++ ?>
            <?php endforeach; ?>
        </table>
    </div>

    <hr>

    <div class="centerTables">


        <label >Resumen de Compra</label>
        <hr>

        <?php echo $this->Form->create('Sale', array('action' => 'add')); ?>
        <?php
        echo $this->Form->hidden('user_id', array('value' => $this->Session->read('Auth.User.id')));
        echo $this->Form->hidden('shop_id', array('value' => $this->Session->read('Auth.User.shop_id')));
        echo $this->Form->hidden('date', array('label' => 'Fecha'));
        ?>
        <div id="divNumber">
            <?php // echo $this->Form->input('ticket_number', array('label' => 'Nro de Ticket', 'readonly')); ?>   
        </div>

        <table cellpadding="0" cellspacing="0" id="tablaFactura" class="tablaFactura" name="data[Sale][table]">
            <tr class="titleTable">
                <th><?php echo 'Promocion' ?></th>
                <th><?php echo 'Cantidad' ?></th>
                <th><?php echo 'Precio Unitario' ?></th>
                <th><?php echo 'Total' ?></th>
                <th><?php echo 'Opciones' ?></th>
            </tr>
        </table>
        <div class="totalGeneral">
            Total: $<p id="totalGeneral" style="display:inline"> </p>
        </div>
        <hr>


        <?php echo $this->Form->end(__('')); ?>
        <?php echo $this->form->button(__('Imprimir Factura'), array('id' => 'imprimir', 'class' => 'imprimir')); ?>
    </div>

</div>

<div class="venta titleTable" >
    Ticket N&#186;     <span id="ticketMenu"> 0 </span> <br>
    Total $ <span id="totalMenu"> 0 </span>
</div>

<div id="divCash" class="caja titleTable" >
</div>

<div id="extras" class="extras titleTable " >
    <p id="addExtra" class="buttonPopup">  Nuevo Gasto Extra</p>

</div>

<?php echo $this->element('extraPayments/popup') ?>

<script>
    var promotion_id;
    var linePromotion = new Array()
    var promotions = new Array()

    $(document).ready(function(){
        

        $('#imprimir').click(function() {
            var user_id = $("#SaleUserId").val();
            var shop_id = $("#SaleShopId").val();
            var date = $("#SaleDate").val();
            var ticket_number = $("#ticket_number").val();
            var cashId = $("#cashId").text();
      
            var cadena ='';
    
            if(promotions.length >0 ){
                //    Convierto el arreglo de productos elegidos a String para enviarlo por post, sino
                //    no me dejaba enviarlo!!!

                for(var i in linePromotion){
                    cadena += linePromotion[i]+'/';
                }
    
                var cadena = cadena.substring(0, cadena.length-1);
    
    
                $.post("<? echo $this->Html->url('addAjax') ?>", 
                { user_id:user_id, shop_id:shop_id, date:date, ticket_number:ticket_number, promotions : cadena, cashId : cashId } ).done(function() { 
                    $dialog.dialog('open');
                    
                    //imprimir ticket
                    $('.venta').printArea();


                })
                .fail(function() { alert("error, Venta no Registrada"); });
         
     
            }else{
                
                $dialogError.dialog('open');
                
            }

        });
        
    });
    
    
    function createTicket(){

        $("#divNumber").load('<? echo $this->Html->url('../Tickets/getNumber') ?>');

    };

    function showCash(){
        var div = $("#divCash");
        div.load('<? echo $this->Html->url('../CashDailies/getCash/' . $this->Session->read('Auth.User.id')) ?>');
        div.show();
    };
    
    
</script>

